﻿#region Copyright

// Majesty of Omega - The next evolution of macro management space strategy
// 
// Copyright 2007-2009 André Claaßen
// 
// This file is protected by the terms and conditions of the
// Microsoft Community License (Ms-CL 1.1), a copy of which should
// have been distributed along with this software. If not,
// you may find the license information at the following URL:
// 
// http://www.microsoft.com/resources/sharedsource/licensingbasics/communitylicense.mspx

#endregion

using System.Collections.Generic;
using System.Linq;
using Majesty_of_Omega.Config;
using Majesty_of_Omega.Model.Research;

namespace Majesty_of_Omega.Logic.Calculate
{
    /// <summary>
    /// </summary>
    public class AppliedResearchProjectCalculator : GameCalculator
    {
        /// <summary>
        /// Called when [calculate].
        /// </summary>
        /// <param name="calculateInfo">The info.</param>
        protected override void OnCalculate(CalculateInfo calculateInfo)
        {
            var researchManager = calculateInfo.Player.ResearchManager;
            foreach (var appliedResearchItem in researchManager.TechCurrentAppliedResearchProjects)
            {
                ResearchFurther(appliedResearchItem);
            }

            // copy the fully researched projects in a temporary list.
            var fullyResearchedProjects = new List<AppliedResearchItem>(
                    researchManager.TechCurrentAppliedResearchProjects.
                                Where(ar => ar.IsFullyResearched));

            foreach (var appliedResearchItem in fullyResearchedProjects)
            {
                researchManager.ApplyFullyResearchedTechnology(appliedResearchItem);
                calculateInfo.ReportTurn.AddSitRepEntry(
                        SitReportType.TechnologyProjectResearched,
                        appliedResearchItem.Technology,
                        "subject", appliedResearchItem.Technology.Name);
            }
        }

        /// <summary>
        /// Researches the current project further. Overruns and roadblocks may occur.
        /// </summary>
        /// <param name="item">The item.</param>
        private static void ResearchFurther(AppliedResearchItem item)
        {
            item.AddResearchForTurn();
        }
    }
}